import { GCAnalytics } from "../../GCAnalytics";
import GCConnection from "../../GCConnection";
import GCGlobal from "../../GCGlobal";
import { AccountResponse } from "../../model/model-account";
import GCBlock from "../common-components/GCBlock";
import GCToast from "../common-components/GCToast";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GCAccount extends cc.Component {

    @property(cc.Label)
    okNode: cc.Label = null;

    @property(cc.EditBox)
    nameEditBox: cc.EditBox = null;

    @property(cc.EditBox)
    passEditBox: cc.EditBox = null;

    @property(cc.Node)
    signinNode: cc.Node = null;

    @property(cc.Node)
    signupNode: cc.Node = null;

    @property(cc.Label)
    accountHelpLbl: cc.Label = null;

    signin() {
        GCAnalytics.loginStart();


        const name = this.nameEditBox.string;
        const password = this.passEditBox.string;

        if (!name || !password || name.length < 3) {
            // toast
            console.error(`illegal parameters!`);
            GCToast.toast('ILLEGAL INPUT!');
            return;
        }

        GCBlock.block();
        GCConnection.signin(name, password)
            .then((resp: AccountResponse) => {
                GCBlock.unblock();

                GCAnalytics.loginSuccess(resp.name, 18, 2);

                if (!cc.isValid(this.node)) {
                    return;
                }

                // set token
                GCGlobal.token = resp.token;
                GCGlobal.Account = resp;
                cc.log(resp);

                cc.director.loadScene('main');
            })
            .catch(e => {
                cc.error(e);
                GCBlock.unblock();
                GCToast.toast(e.message);
            });
    }

    signup() {
        GCAnalytics.loginStart();

        const name = this.nameEditBox.string;
        const password = this.passEditBox.string;

        if (!name || !password || name.length < 3) {
            // toast
            console.error(`illegal parameters!`);
            GCToast.toast('ILLEGAL INPUT!');
            return;
        }

        GCBlock.block();
        GCConnection.signup(name, password)
            .then((resp: AccountResponse) => {
                GCBlock.unblock();

                GCAnalytics.loginSuccess(resp.name, 18, 2);

                if (!cc.isValid(this.node)) {
                    return;
                }

                // set token
                GCGlobal.token = resp.token;
                GCGlobal.Account = resp;

                cc.log(resp);

                cc.director.loadScene('main');
            })
            .catch(e => {
                GCBlock.unblock();
                GCToast.toast('SignUp Failed! Try It Later!');
            });
    }

    onNameTextDidBegin() {
        this.okNode.string = '';
        this.okNode.node.active = false;
    }

    onNameTextChanged(text: string) {
        cc.log(`onNameTextChanged: ${text}`);
    }

    onNameEditDidEnded(editbox: cc.EditBox) {
        cc.log(`onNameEditDidEnded: ${editbox.string.length}`);

        // if (editbox.string.length < 5) {
        //     this.okNode.string = 'not ok';
        //     this.okNode.node.active = true;
        //     return;
        // }

        // GCConnection.validate(editbox.string).then(res => {
        //     if (!cc.isValid(this.node)) {
        //         return;
        //     }

        //     this.okNode.string = 'ok';
        //     this.okNode.node.active = true;
        // }).catch(e => {
        //     this.okNode.string = 'not ok';
        //     this.okNode.node.active = true;
        // })
    }

    onNameEditingReturn(editbox: cc.EditBox) {
        // will also triggle onNameEditDidEnded
        cc.log(`onNameEditingReturn`);
    }

    private _signup = false;
    onSwitchSignUp(ev: cc.Event.EventTouch) {
        this.signinNode.active = this._signup;
        this.signupNode.active = !this._signup;
        this.accountHelpLbl.string = this._signup ? 'No Account?' : 'Already Had Account?';
        this._signup = !this._signup;
    }

    onSignInBtnClicked() {
        this.signin();
    }

    onSignUpBtnClicked() {
        this.signup();
    }
}
